package ro.tm.uvt.lp.raccoon.collectorGame;

import java.util.Iterator;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

import ro.tm.uvt.lp.raccoon.SaveTheRaccoon;

public class CollectorGameScreen implements Screen {

    SaveTheRaccoon game;

    Texture berryImage;
    Texture raccoonImage;
    Sound twigSound;
    Music raccoonCallingMusic;
    OrthographicCamera camera;
    Rectangle raccoon;
    Array<Rectangle> berries;
    long lastBerryTime;
    int berriesGathered;

    public CollectorGameScreen(Game gam) {
        this.game =(SaveTheRaccoon) gam;

// load the images for the droplet and the raccoon, 64x64 pixels each
        berryImage = new Texture(Gdx.files.internal("berry.png"));
        raccoonImage = new Texture(Gdx.files.internal("raccoon.png"));

// load the drop sound effect and the rain background "music"
        twigSound = Gdx.audio.newSound(Gdx.files.internal("twig.mp3"));
        raccoonCallingMusic = Gdx.audio.newMusic(Gdx.files.internal("raccoonCalling.mp3"));
        raccoonCallingMusic.setLooping(true);

// create the camera and the SpriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

// create a Rectangle to logically represent the raccoon
        raccoon = new Rectangle();
        raccoon.x = 800 / 2 - 64 / 2; // center the raccoon horizontally
        raccoon.y = 20; // bottom left corner of the raccoon is 20 pixels above
// the bottom screen edge
        raccoon.width = 64;
        raccoon.height = 64;

// create the berries array and spawn the first raindrop
        berries = new Array<Rectangle>();
        spawnBerry();

    }

    private void spawnBerry() {
        Rectangle berry = new Rectangle();
        berry.x = MathUtils.random(0, 800 - 64);
        berry.y = 480;
        berry.width = 64;
        berry.height = 64;
        berries.add(berry);
        lastBerryTime = TimeUtils.nanoTime();
    }

    @Override
    public void render(float delta) {
// clear the screen with a dark blue color. The
// arguments to glClearColor are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

// tell the camera to update its matrices.
        camera.update();

// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
        game.getBatch().setProjectionMatrix(camera.combined);

// begin a new batch and draw the raccoon and
// all drops
        game.getBatch().begin();
        game.getFont().draw(game.getBatch(), "Berries eaten: " + berriesGathered, 0, 480);
        game.getBatch().draw(raccoonImage, raccoon.x, raccoon.y);
        for (Rectangle berry : berries) {
            game.getBatch().draw(berryImage, berry.x, berry.y);
        }
        game.getBatch().end();

// process user input
        if (Gdx.input.isTouched()) {
            Vector3 touchPos = new Vector3();
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(touchPos);
            raccoon.x = touchPos.x - 64 / 2;
        }
//        if (Gdx.input.isKeyPressed(Keys.LEFT))
//            raccoon.x -= 200 * Gdx.graphics.getDeltaTime();
//        if (Gdx.input.isKeyPressed(Keys.RIGHT))
//            raccoon.x += 200 * Gdx.graphics.getDeltaTime();

// make sure the raccoon stays within the screen bounds
        if (raccoon.x < 0)
            raccoon.x = 0;
        if (raccoon.x > 800 - 64)
            raccoon.x = 800 - 64;

// check if we need to create a new raindrop
        if (TimeUtils.nanoTime() - lastBerryTime > 1000000000)
            spawnBerry();

// move the berries, remove any that are beneath the bottom edge of
// the screen or that hit the raccoon. In the later case we play back
// a sound effect as well.
        Iterator<Rectangle> iter = berries.iterator();
        while (iter.hasNext()) {
            Rectangle berry = iter.next();
            berry.y -= 200 * Gdx.graphics.getDeltaTime();
            if (berry.y + 64 < 0)
                iter.remove();
            if (berry.overlaps(raccoon)) {
                berriesGathered++;
//                twigSound.play();
                iter.remove();
            }
        }
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
// start the playback of the background music
// when the screen is shown
        raccoonCallingMusic.play();
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        berryImage.dispose();
        raccoonImage.dispose();
        twigSound.dispose();
        raccoonCallingMusic.dispose();
    }

}
